package br.com.wepa.webapps.orca.controle.actions.grupos;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import br.com.wepa.webapps.logger.TraceLogger;
import br.com.wepa.webapps.orca.controle.actions.struts.GenericAction;
import br.com.wepa.webapps.orca.logica.negocio.facade.FacadeDelegator;
import br.com.wepa.webapps.orca.logica.negocio.facade.GruposFacadeHome;
import br.com.wepa.webapps.orca.logica.negocio.facade.basic.GenericFacadeHome;
import br.com.wepa.webapps.orca.logica.negocio.facade.to.GrupoTO;

public class GruposAction extends GenericAction  {
	private static TraceLogger logger = new TraceLogger(GruposAction.class);
	public static final String LIST_TILTE_KEY = "list_title";
	public static final String DETAIL_TILTE_KEY = "detail_title";
	public static final String FORM_TILTE_KEY = "form_title";
	
	
	private GruposFacadeHome facade = null;

	@Override
	protected GenericFacadeHome getFacade() {
		return facade();
	}
	public GruposAction() {
		super();
	}
	private GruposFacadeHome facade() {
		if(facade == null){
			facade =  FacadeDelegator.getGruposFacade();
		}
		return facade;
	}
	
	
	@Override
	public ActionForward goCadastrar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.entering();
		super.saveToken(request);
		GrupoTO to = (GrupoTO)getGenericTansferObjectInRequest(request);		
		super.copyFormToObject(to, form);
		
		to.setUpdate(false);
		to = facade().setUp(to);
		super.clearTansferObject(request);		
		super.setTansferObject(request, to, false);
		super.clearForm(form);

		if(to.ehNivelGrupo()){
			request.setAttribute(FORM_TILTE_KEY, "grupo.entity");
		}else if(to.ehNivelSubGrupo()){
			to.setTemPai(true);
			request.setAttribute(FORM_TILTE_KEY, "subgrupo.entity");
		}
		super.pushActionEvent(request,form,mapping, mapping
				.findForward(FORWARD_GO_CADASTRAR));
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_CADASTRO);
	}
	
	
	@Override
	public ActionForward goAltualizar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		logger.entering();
		super.saveToken(request);

		GrupoTO to = (GrupoTO) getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);

		to.setUpdate(true);
		to.setLock(true);
		to = facade().setUp(to);

		super.clearAndSetTansferObject(request, to, true);
		super.copyObjectToForm(form, to);

		if (to.ehNivelGrupo()) {
			request.setAttribute(FORM_TILTE_KEY, "grupo.entity");
		} else if (to.ehNivelSubGrupo()) {
			to.setTemPai(true);
			request.setAttribute(FORM_TILTE_KEY, "subgrupo.entity");
		}
		super.pushActionEvent(request, form, mapping);
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_CADASTRO);
	}

	@Override
	public ActionForward goDetalhar(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		logger.entering();
		super.saveToken(request);

		GrupoTO to = (GrupoTO) getGenericTansferObjectInRequest(request);
		super.copyFormToObject(to, form);
		
		to = facade().load(to, false);
		
		super.clearAndSetTansferObject(request, to, false);
		if(to.ehNivelGrupo()){
			request.setAttribute(DETAIL_TILTE_KEY, "grupo.entity");
			to.setTemFilhos(true);
			request.setAttribute(LIST_TILTE_KEY, "subgrupo.list.title");
		}else if(to.ehNivelSubGrupo()){
			to.setTemPai(true);
			to.setTemFilhos(false);
			request.setAttribute(DETAIL_TILTE_KEY, "subgrupo.entity");
		}
		super.pushActionEvent(request,form, mapping,mapping
						.findForward(FORWARD_GO_DETALHAR));
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_DETALHA);
	}
	
	@Override
	public ActionForward goListar(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		logger.entering();
		super.saveToken(request);
		GrupoTO to = (GrupoTO) getGenericTansferObjectInRequest(request);
		to.setPagingBean(super.configPaging(request, form, mapping));

		super.copyFormToObject(to, form);

		to = facade().findAllByNivel(to);

		super.clearAndSetTansferObject(request, to, false);
		if (to.ehNivelGrupo()) {
			request.setAttribute(LIST_TILTE_KEY, "grupo.list.title");
		} else if (to.ehNivelSubGrupo()) {
			to.setTemPai(true);
			request.setAttribute(LIST_TILTE_KEY, "subgrupo.list.title");
		}
		super.pushActionEvent(request, form, mapping, mapping
				.findForward(FORWARD_GO_LISTAR));
		logger.exiting();
		return mapping.findForward(FORWARD_PAGE_LISTA);
	}
	
	
	@Override
	public ActionForward remover(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		logger.entering();

		GrupoTO to = (GrupoTO) getGenericTansferObjectInRequest(request);
		
		ActionForward forward = super.remover(mapping, form, request, response);
		super.copyFormToObject(to, form);
		if (forward.getPath().indexOf(FORWARD_GO_DETALHAR) != -1) {
			forward = mapping.findForward(GLOBAL_FWPAGE_exibeMsg);			
		}
		logger.exiting();
		return forward;
	}
		
	@Override
	protected String getForwardErroName(String nomeMetodo) {
		// TODO Auto-generated method stub
		return null;
	}

}